{extend name="property/public/insidePageBase" /}
{block name="title"}系统菜单列表{/block}
{block name="body"}
<div class="layuimini-container">
    <div class="layuimini-main">
        <div>
            <script type="text/html" id="toolbar">
                <div class="layui-btn-container">
                    <div class="layui-btn-group">
                        <button class="layui-btn layui-btn-sm" id="btn-expand">全部展开</button>
                        <button class="layui-btn layui-btn-sm layui-btn-normal" id="btn-fold">全部折叠</button>
                    </div>
                    {if $addAuth}<button class="layui-btn layui-btn-normal layui-btn-sm" lay-event="add" style="margin-left: 10px;"> 添加 </button> {/if}
                </div>
            </script>
            <table id="menu-table" class="layui-table" lay-filter="menu-table"></table>
        </div>
    </div>
</div>
<script type="text/html" id="auth-state">
    <div class="layui-btn-group">
        {if $editAuth} <a class="layui-btn layui-btn-sm  layui-btn-normal" lay-event="edit">编辑</a>{/if}
        {if $delAuth}<a class="layui-btn layui-btn-sm layui-btn-danger data-count-delete" lay-event="delete">删除</a>{/if}
    </div>
</script>
{/block}
{block name="js"}
{__block__}
<script>
    layui.use(['table', 'treetable'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var treetable = layui.treetable;
        var tableId = 'menu-table';
        // 渲染表格
        layer.load(2);
        treetable.render({
            treeColIndex: 1,
            treeSpid: -1,
            treeIdName: 'id',
            treePidName: 'pid',
            toolbar: '#toolbar',
            elem: '#'+tableId,
            id:tableId,
            url: '{:url("propertyAuthRuleList")}',
            page: false,
            treeDefaultClose:true,
            cols: [[
                {field: 'id', width: 88, title: 'ID'},
                {field: 'name', width: 258, title: '权限名称'},
                {field: 'icon', width: 80, align:'center', title: '图标',templet:function(d) {
                    return '<i class="'+d.icon+'"></i>'
                    }},
                {field: 'rule_url',width: 258,  title: '菜单url'},
                {field: 'sort', width: 80, align: 'center', title: '排序号'},
                {
                    field: 'is_menu', width: 80, align: 'center', templet: function (d) {
                        if (d.is_menu != 1) {
                            return '<span class="layui-badge layui-bg-gray">按钮</span>';
                        }
                        if (d.pid == -1) {
                            return '<span class="layui-badge layui-bg-blue">目录</span>';
                        } else {
                            return '<span class="layui-badge-rim">菜单</span>';
                        }
                    }, title: '类型'
                }
                ,{templet: '#auth-state', minWidth: 168,  title: '操作', fixed:'right'}
            ]],
            done: function () {
                layer.closeAll('loading');
            }
        });

        $(document).on('click', '#btn-expand', function () {
            treetable.expandAll('#'+tableId);
        });

        $(document).on('click', '#btn-fold', function () {
            treetable.foldAll('#'+tableId);
        });


        /**
         * toolbar监听事件
         */
        table.on('toolbar('+tableId+')', function(obj) {
            if (obj.event === 'add') { // 添加
                var url = '{:url("propertyAuthRuleAdd")}';
                var index = parent.layer.open({
                    title: '添加菜单', type: 2, shade: 0.2, maxmin: true, area: ['600px', '600px'],  content: url, btnAlign: 'c', btn: ['确定', '取消'], yes: function(index, layero) {
                        var iframeWindow = parent.window['layui-layer-iframe' + index], submit = layero.find('iframe').contents().find("#submitBtn");
                        iframeWindow.layui.form.on('submit(submitBtn)', function(data){
                            var field = data.field;//获取提价的数据
                            var loadAdd = parent.layer.msg('提交中', {icon:16, time:0, shade:0.1, offset:'15px'});
                            $.post(url, field, function(res){
                                parent.layer.close(loadAdd);
                                if (res.code === 0) {
                                    location.reload()
                                    parent.layer.close(index); //关闭弹层
                                    parent.layer.msg(res.msg, {icon: 6, offset: '15px'});
                                } else {
                                    parent.layer.msg(res.msg, {icon: 5, offset: '15px'});
                                }
                            })
                        })
                        submit.trigger('click');
                    }
                })
                return false;
            }
        });

        //监听工具条
        table.on('tool('+tableId+')', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') {
                var url = '{:url("propertyAuthRuleEdit")}?id=' + data.id;
                var index = layer.open({ title: '修改菜单', type: 2,  shade: 0.2,  maxmin:true, area: ['500px', '400px'],  content: url , btnAlign: 'c'   , btn: ['确定', '取消']  , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index], submit = layero.find('iframe').contents().find("#submitBtn");
                        iframeWindow.layui.form.on('submit(submitBtn)', function (data) {
                            var field = data.field; //获取提交的字段
                            var loadAdd = layer.msg('提交中', {icon: 16, time: 0, shade: 0.1, offset: '15px'});
                            $.post(url, field, function (res) {
                                layer.close(loadAdd);
                                if (res.code === 0) {
                                    location.reload()
                                    layer.close(index); //关闭弹层
                                   layer.msg(res.msg, {icon: 6, offset: '15px'});
                                } else {
                                    layer.msg(res.msg, {icon: 5, offset: '15px'});
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if(obj.event === 'delete') {
                var url = '{:url("propertyAuthRuleDel")}';
                layer.confirm('删除后不可恢复！是否确定？',{icon:3}, function(){
                    var loadAdd = layer.msg('提交中',{icon:16, time:0, shade: 0.1, offset:"15px"})
                    $.post(url, {id:data.id}, function(res){
                        layer.close(loadAdd)
                        if(  res.code === 0 ){
                            location.reload()
                            table.reload(tableId)
                            layer.msg(res.msg,{icon:6, offset: '15px'})
                        }else {
                            layer.msg(res.msg, {icon:5, offset: '15px'})
                        }
                    })
                })
            }
        });
    });
</script>
{/block}